USE [Gestion2]
GO

/****** Object:  StoredProcedure [dbo].[WASP_D0009_INSERTA_DATOS_A_TABLA]    Script Date: 11/22/2011 16:09:01 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[WASP_D0009_INSERTA_DATOS_A_TABLA]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[WASP_D0009_INSERTA_DATOS_A_TABLA]
GO

USE [Gestion2]
GO

/****** Object:  StoredProcedure [dbo].[WASP_D0009_INSERTA_DATOS_A_TABLA]    Script Date: 11/22/2011 16:09:01 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[WASP_D0009_INSERTA_DATOS_A_TABLA] 
(
@fecha_inicio as char(8)
)
AS
BEGIN

insert into seg_salud 
select 
      fsd611.ppcta,  
      '',
      '',
      fsd611.ppsuc,
      fsd611.ppoper,
      0,
      0,
      count(*),
      '',
      ''
from 
	fsd611 (nolock), 
	jt73109 (nolock), 
	fsd601  (nolock)
where     (fsd611.ppimp11 =  106  or
	   fsd611.ppimp12 = 106  or
	   fsd611.ppimp13 = 106  or
                fsd611.ppimp14 = 106  or
                fsd611.ppimp15 = 106  or
                fsd611.ppimp16 = 106  or
                fsd611.ppimp17 = 106  or
                fsd611.ppimp18 = 106  or
                fsd611.ppimp19 = 106  ) and
                jt73109.jt73109fpa   = fsd611.ppoper and
                jt73109.jt73109suc  = fsd611.ppsuc and
                 jt73109.jt73109FAP  = @fecha_inicio  and 
--                jt73109.jt73109FAP between ? and ? and
	   fsd611.pgcod  = fsd601.pgcod and
	   fsd611.ppmod  = fsd601.ppmod and
	   fsd611.ppsuc  = fsd601.ppsuc and 
	   fsd611.ppmda  = fsd601.ppmda and
	   fsd611.ppcta  = fsd601.ppcta and 
	   fsd611.ppoper = fsd601.ppoper and
	   fsd611.ppsbop = fsd601.ppsbop and
	   fsd611.pptope = fsd601.pptope and
	   fsd601.d601co = 'S' and 
	   fsd611.pptipo = '' and
           fsd611.ppexte = 9 
group by fsd611.ppsuc, fsd611.ppcta, fsd611.ppoper



update seg_salud set  INICIO_COBERTURA = cast(convert( varchar, Aofval,112)as decimal(8,0)), 
	              FIN_COBERTURA    = cast(convert( varchar, dateadd(day, -1, dateadd(year,1, aofval)),112)as decimal(8,0)) 
from  fsd010 
where rut         = aocta and
      agcoloc     = aosuc and 
      nro_credito = aooper and
      aosbop = 0

update seg_salud set DV     = substring(pfndoc, len(pfndoc),1 ),
                     NOMBRE = rtrim(ltrim(pfape1)) +' '+ rtrim(ltrim(pfape2)) +' '+ pfnom1
from  fsr008,
      fsd002
where ctnro = rut and 
      Pendoc = Pfndoc

update seg_salud set TLFONOPEN = dotelfp
from 
       fsr005
where 
       Pendoc = rtrim(ltrim(rut)) +''+ rtrim(ltrim(dv)) and
       Doordp <> 61 and dotelfp <> ''

update  seg_salud set DIRTRABAJA = convert(varchar(40),rtrim(ltrim(Z07331NOM))+', '+ rtrim(ltrim(Z07310DSC)))
from z07331,
     z07310
where z07331.Z07331CTA = seg_salud.rut and
      z07331.z07310COM = z07310.Z07310COM 

END

GO

